package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.example.demo.constant.SystemConstant;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.AvatarDTO;
import com.example.demo.dto.ClothingDTO;
import com.example.demo.dto.CommodityDTO;
import com.example.demo.mapper.entity.Avatar;
import com.example.demo.mapper.entity.Clothing;
import com.example.demo.service.StudentClothingService;
import com.example.demo.utils.PageBo;
import com.example.demo.utils.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lucy
 * @create 2024-02-26 19:37
 */
@RequestMapping("dressUp")
@Api(value = "换装功能接口", tags = {"换装功能接口"})
@RestController
@Slf4j
public class DressUpController {

    @Autowired
    private StudentClothingService clothingService;

    @ApiOperation("获取我的衣柜发型列表")
    @RequestMapping(value = "getHairstyles", method = {RequestMethod.POST})
    public Result<PageVo<ClothingDTO>> getHairstyles(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<ClothingDTO> HairstylesList =  clothingService.getClothingList(pageBo, studentId, TaskConstant.MY_WARDROBE_HAIRSTYLES);
        return Result.ok(HairstylesList);
    }

    @ApiOperation("获取我的衣柜上衣列表")
    @RequestMapping(value = "getTops", method = {RequestMethod.POST})
    public Result<PageVo<ClothingDTO>> getTops(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<ClothingDTO> TopsList =  clothingService.getClothingList(pageBo, studentId, TaskConstant.MY_WARDROBE_TOPS);
        return Result.ok(TopsList);
    }

    @ApiOperation("获取我的衣柜下装列表")
    @RequestMapping(value = "getBottoms", method = {RequestMethod.POST})
    public Result<PageVo<ClothingDTO>> getBottoms(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<ClothingDTO> BottomsList =  clothingService.getClothingList(pageBo, studentId, TaskConstant.MY_WARDROBE_BOTTOMS);
        return Result.ok(BottomsList);
    }

    @ApiOperation("获取我的衣柜鞋子列表")
    @RequestMapping(value = "getShoes", method = {RequestMethod.POST})
    public Result<PageVo<ClothingDTO>> getShoes(@RequestBody PageBo pageBo){
        int studentId = StpUtil.getLoginIdAsInt();
        PageVo<ClothingDTO> ShoesList =  clothingService.getClothingList(pageBo, studentId, TaskConstant.MY_WARDROBE_SHOES);
        return Result.ok(ShoesList);
    }

    @ApiOperation("获取当前装扮")
    @RequestMapping(value = "getCurrentDress", method = {RequestMethod.POST})
    public Result<List<AvatarDTO>> getCurrentDress(){
        int studentId = StpUtil.getLoginIdAsInt();
        List<Avatar> avatars = clothingService.selectAvatarListByStudentId(studentId);
        List<AvatarDTO> avatarDTOList = new ArrayList<>();
        for(Avatar avatar : avatars){
            AvatarDTO dto = new AvatarDTO();
            dto.setClothingType(avatar.getClothingType());
            dto.setClothingId(avatar.getClothingId());
            dto.setImageUrl(SystemConstant.aliyunOSSUrlPrefix + avatar.getImageUrl());
            avatarDTOList.add(dto);
        }
        return Result.ok(avatarDTOList);
    }

    @ApiOperation("保存当前装扮")
    @RequestMapping(value = "saveCurrentDress", method = {RequestMethod.POST})
    public Result<String> saveCurrentDress(@RequestBody List<Integer> clothingIdList){
        int studentId = StpUtil.getLoginIdAsInt();
        //删除历史装扮
        clothingService.deleteAvatarListByStudentId(studentId);
        //利用clothingId组装数据，将组装好的avatar插入数据库
        for(Integer clothingId : clothingIdList){
            Clothing clothing = clothingService.selectClothingById(clothingId);
            Avatar avatar = new Avatar();
            avatar.setStudentId(studentId);
            avatar.setClothingId(clothing.getClothingId());
            avatar.setClothingType(clothing.getType());
            avatar.setImageUrl(clothing.getImageUrl());
            clothingService.insertAvatar(avatar);
        }
        return Result.ok("保存成功");
    }






}
